package com.cskaoyan.service;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.comment.MarketComment;
import com.cskaoyan.bean.comment.MarketCommentExample;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wxcomment.CommentVo;
import com.cskaoyan.bean.wxcomment.PostBean;
import com.cskaoyan.bean.wxcomment.WxCommentsBo;
import com.cskaoyan.bean.wxcomment.WxCommentsCountVo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.WxCommentMapper;
import com.cskaoyan.util.DateUtils;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * \* Description:
 * \
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {


    @Autowired
    WxCommentMapper wxCommentMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @SuppressWarnings("all")
    @Override
    public BaseData<CommentVo> comment(WxCommentsBo wxCommentsBo) {
        //获取用户信息
        String primaryPrincipal = (String) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        User user = wxCommentMapper.getUserInfo(primaryPrincipal);
        CommentVo.UserInfoBean userInfoBean = new CommentVo.UserInfoBean();
        userInfoBean.setAvatarUrl(user.getAvatar());
        userInfoBean.setNickName(user.getNickname());
        //分页预编译
        PageUtil.start(wxCommentsBo);
        ArrayList<CommentVo> commentVos = new ArrayList<>();
        //条件查询
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

//增加条件
        criteria.andValueIdEqualTo(wxCommentsBo.getValueId());
        int type = wxCommentsBo.getType();
        criteria.andTypeEqualTo((byte) type);

        //全部查询和只查询有图
        if (wxCommentsBo.getShowType() == 0) {
            List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
//            全部查询
            for (MarketComment marketComment : marketComments) {
                CommentVo commentVo = new CommentVo();
                commentVo.setUserInfo(userInfoBean);
                Date addTime = marketComment.getAddTime();
                String date = DateUtils.dateFormat(addTime);
                commentVo.setAddTime(date);
                commentVo.setAdminContent(marketComment.getAdminContent());
                commentVo.setContent(marketComment.getContent());
                String[] picUrls = marketComment.getPicUrls();
                commentVo.setPicList(picUrls);
                commentVos.add(commentVo);
            }
        } else if (wxCommentsBo.getShowType() == 1) {
//            查询有图

            criteria.andHasPictureEqualTo(true);
            List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
            for (MarketComment marketComment : marketComments) {
                CommentVo commentVo = new CommentVo();
                commentVo.setUserInfo(userInfoBean);
                Date addTime = marketComment.getAddTime();
                String date = DateUtils.dateFormat(addTime);
                commentVo.setAddTime(date);
                commentVo.setAdminContent(marketComment.getAdminContent());
                commentVo.setContent(marketComment.getContent());
                String[] picUrls = marketComment.getPicUrls();
                commentVo.setPicList(picUrls);
                commentVos.add(commentVo);
            }
        }


        PageInfo<CommentVo> marketCommentPageInfo = new PageInfo<>(commentVos);
        BaseData<CommentVo> objectBaseData = new BaseData<>(marketCommentPageInfo, commentVos);
        return objectBaseData;
    }

    @Override
    public WxCommentsCountVo count(Integer valueId) {
        int allCount = wxCommentMapper.allCount(valueId);
        int hasPicCount = wxCommentMapper.hasPicCount(valueId);
        WxCommentsCountVo wxCommentsCountVo = new WxCommentsCountVo();
        wxCommentsCountVo.setAllCount(allCount);
        wxCommentsCountVo.setHasPicCount(hasPicCount);

        return wxCommentsCountVo;
    }

    @Override
    public void post(PostBean postBean) {
        MarketComment marketComment = new MarketComment();
        marketComment.setType(postBean.getType());
        marketComment.setAddTime(new Date(System.currentTimeMillis()));
        marketComment.setContent(postBean.getContent());
        marketComment.setStar(postBean.getStar());
        marketComment.setValueId(postBean.getValueId());
        marketComment.setHasPicture(postBean.isHasPicture());
        marketComment.setPicUrls(postBean.getPicUrls());
        String primaryPrincipal = (String) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        int id = wxCommentMapper.getUserId(primaryPrincipal);
        marketComment.setUserId(id);

        int insert = marketCommentMapper.insert(marketComment);

    }
}
